#============================================================
#	Item Info
#============================================================
# @datetime: 2022-5-15 02:41:16
#============================================================
extends Panel


#============================================================
#   内置
#============================================================
func _ready():
	WrapperHelper.get_custom_signal(Global.player) \
		.connect_custom_signal(CustomSignal.DetectItem, self, "_detect_item")
	WrapperHelper.get_custom_signal(Global.player) \
		.connect_custom_signal(CustomSignal.ExitItem, self, "_exit_item")
	
	WrapperHelper.get_equipment(Global.player) \
		.connect("loaded", self, "_loaded")
	
	update_info()


#============================================================
#   自定义
#============================================================
func _loaded(data):
	update_info()


var _item_nodes = []
func _detect_item(item: BaseItem):
	_item_nodes.append(item)
	update_info()

func _exit_item(_item: BaseItem):
	if _item_nodes.has(_item):
		_item_nodes.erase(_item)
	
	update_info()


onready var _texture_rect = get_node("HBoxContainer/Control/TextureRect")
onready var _item_name = get_node("HBoxContainer/VBoxContainer/ItemName")
onready var _item_description = get_node("HBoxContainer/VBoxContainer/ItemDescription") 
##  更新信息
func update_info():
	if _item_nodes.size() > 0:
		var item : BaseItem = Node2DUtil.get_closest_node(Global.player, _item_nodes)
		var weapon : WeaponBean = JsonUtil.dict2object(item.get_data(), WeaponBean)
		_item_name.text = weapon.name
		_item_description.text = weapon.desc
		_texture_rect.texture = weapon.texture
		show()
	else:
		hide()

